﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Windows;

namespace YSGetLatest.DataObjects
{
	/// <summary>
	/// Holds data about projects.
	/// </summary>
	[Serializable]
	public class Projects : BaseDataObject
	{
		#region Fields

		private ObservableCollection<Project> projectList_;
		private ObservableCollection<Project> projectListSaved_;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the list of the projects.
		/// </summary>
		public ObservableCollection<Project> ProjectList
		{
			get
			{
				if (projectList_ == null)
				{
					projectList_ = new ObservableCollection<Project>();
				}
				return projectList_;
			}
		}
		#endregion

		#region BaseDataObject Methods

		/// <summary>
		/// Saves data members.
		/// </summary>
		protected override void Save()
		{
			projectListSaved_ = new ObservableCollection<Project>(ProjectList.ToList());

			// Save old values for each project property.
			foreach (var project in projectListSaved_)
			{
				project.BeginEdit();
			}
		}

		/// <summary>
		/// Restores data members.
		/// </summary>
		protected override void Restore()
		{
			// Restore old values for each project property.
			foreach (var project in projectListSaved_)
			{
				project.CancelEdit();
			}

			projectList_ = new ObservableCollection<Project>(projectListSaved_.ToList());

			SetPropertyChanged("ProjectList");
		}
		#endregion

		#region Methods

		/// <summary>
		/// Returns the text representation of the instance.
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			var buffer = new StringBuilder();

			buffer.AppendFormat("{0}:", GetType().Name);
			buffer.Append(" ProjectList:[");

			bool firstItem = true;
			foreach (var project in ProjectList)
			{
				if (!firstItem)
				{
					buffer.Append(", ");
				}
				else
				{
					firstItem = false;
				}
				buffer.Append(project.ToString());
			}
			buffer.Append("]");

			return buffer.ToString();
		}

		/// <summary>
		/// Loads data from file.
		/// </summary>
		public void LoadProjects()
		{
			bool fileDoesNotExist;
			var fileName = FileUtil.GetStorageFileName(out fileDoesNotExist);

			if (!fileDoesNotExist)
			{
				try
				{
					using (var reader = new XmlTextReader(new StreamReader(fileName, Encoding.UTF8)))
					{
						var xmlReaderSettings = new XmlReaderSettings
						{
							CloseInput = true,
						};
						var xmlReader = XmlReader.Create(reader, xmlReaderSettings);
						var xmlSerializer = new XmlSerializer(GetType());
						var theProjects = (Projects)xmlSerializer.Deserialize(reader);
						foreach (var project in theProjects.ProjectList)
						{
							ProjectList.Add(project);
						}
						reader.Close();
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
			}
		}

		/// <summary>
		/// Stores data to file.
		/// </summary>
		public void SaveProjects()
		{
			bool fileDoesNotExist;
			var fileName = FileUtil.GetStorageFileName(out fileDoesNotExist);

			using (var writer = new XmlTextWriter(fileName, Encoding.UTF8))
			{
				var xmlWriterSettings = new XmlWriterSettings
				{
					Indent = true,
					Encoding = Encoding.UTF8
				};
				var xmlWriter = XmlWriter.Create(writer, xmlWriterSettings);
				var xmlSerializer = new XmlSerializer(GetType());
				xmlSerializer.Serialize(xmlWriter, this);
				writer.Close();
			}
		}
		#endregion
	}
}
